package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.ArticleDTO;
import com.ruoyi.system.domain.WaImage;
import com.ruoyi.system.domain.WaNotice;
import com.ruoyi.system.domain.vo.ArticleVo;
import com.ruoyi.system.mapper.WaNoticeMapper;
import com.ruoyi.system.service.WaNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@Service
public class WaNoticeServiceImpl implements WaNoticeService {
    @Autowired
    private WaNoticeMapper noticeMapper;
//查询通知列表
    @Override
    public List<WaNotice> selectNoticeList(WaNotice notice) {
        return noticeMapper.selectNoticeList(notice);
    }
    //通过栏目id查找文章
    @Override
    public List<WaNotice> selectArticleByColumnById(WaNotice query) {
        return noticeMapper.selectArticleByColumnById(query);
    }


    //添加通知公告列表
    @Override
    public int insertNotice(WaNotice notice) {
        LocalDateTime currentTime = LocalDateTime.now();
        Date currentDate = Date.from(currentTime.atZone(ZoneId.systemDefault()).toInstant());
        notice.setCreateTime(currentDate);
        notice.setPublishTime(currentDate);
        return noticeMapper.insertNotice(notice);
    }
    //判读文章id是否存在
    @Override
    public boolean checkArticleExists(Long articleId) {
        if (articleId == null) {
            return false; // ID为null时，直接判定为不存在
        }
        WaNotice query = new WaNotice();
        query.setArticleId(articleId);
        List<WaNotice> list = noticeMapper.selectNoticeList(query);
        // 修正逻辑：list不为空且包含数据（即存在该文章ID），返回true
        return list != null && !list.isEmpty();
    }

//检查文章id是否已经被使用
    @Override
    public boolean isArticleIdUsedNotice(Long articleId) {
        if (articleId == null || articleId <= 0) {
            return false;
        }
      WaNotice query = new WaNotice();
        query.setArticleId(articleId);
        List<WaNotice> imageList = noticeMapper.selectNoticeList(query);
        return imageList != null && !imageList.isEmpty();
    }
//通过发布时间获取栏目列表
    @Override
    public List<WaNotice> selectArticleByPublishTime(WaNotice query) {
        return  noticeMapper.selectArticleByPublishTime(query);
    }

    @Override
    public List<ArticleVo> selectArticle(ArticleDTO dto) {
        return noticeMapper.selectArticle(dto);
    }


    //删除通知公告列表
    @Override
    public int deleteNoticeById(Long noticeId) {
        return noticeMapper.deleteNoticeById(noticeId);
    }

}
